﻿/**
 * pixSWX is SWXformat API for pixLib
 *  
 * Copyright © 2007 Laurent Deketelaere, Aral Balkan, Francis Bourre
 * Part of the SWX (SWF Data Exchange Format) project. http://swxformat.org
 * Part of the pixLib project. http://pixlib.org
 *
 * Released under the open source MIT license.
 * http://opensource.org/licenses/mit-license.php
 */
import org.triptyk.pixswx.EncodingList;
import org.triptyk.pixswx.ISWXListener;
import org.triptyk.pixswx.JsonSerialize;
import org.triptyk.pixswx.SWXConnection;
import org.triptyk.pixswx.SWXFaultEvent;
import org.triptyk.pixswx.SWXResultEvent;

import com.bourre.data.libs.AbstractLib;
import com.bourre.events.EventType;
import com.bourre.remoting.ServiceResponder;

class org.triptyk.pixswx.SWXCall	extends AbstractLib {	private var _sServiceName : String;	private var _sMethod : String;	private var _aArgs : Array;
	private var _bDebug : Boolean;	private var _sEncoding : String;
	
	public static var onLoadInitEVENT : EventType = AbstractLib.onLoadInitEVENT;	public static var onLoadProgressEVENT : EventType = AbstractLib.onLoadProgressEVENT;	public static var onTimeOutEVENT : EventType = AbstractLib.onTimeOutEVENT;	public static var onErrorEVENT : EventType = AbstractLib.onErrorEVENT;

	public static var onResultEVENT : EventType = ServiceResponder.onResultEVENT;
	public static var onFaultEVENT : EventType = ServiceResponder.onFaultEVENT;

	public function SWXCall( sURL : String, sFullyQualifiedMethodName : String ) 	{		super();				setURL(sURL);
		// get MovieClip for connexion 
		setContent(SWXConnection.getInstance().getConnexion());		setFullyQualifiedMethodName(sFullyQualifiedMethodName);
		// -- 
		_bDebug = true;
		_sEncoding = EncodingList.GET;	}

	public function setFullyQualifiedMethodName( sFullyQualifiedMethodName : String ) : Void	{		if ( sFullyQualifiedMethodName )		{			var a : Array = sFullyQualifiedMethodName.split(".");			setMethodName(String(a.pop()));			setServiceName(a.join("."));		}	}
	
	public function setServiceName( serviceName : String ) : Void	{		_sServiceName = serviceName;	}
	
	public function setMethodName( serviceMethod : String ) : Void	{		_sMethod = serviceMethod;	}

	public function setEncoding( sEncoding : String ) : Void
	{
		_sEncoding = sEncoding;
	}

	public function getEncoding( Void ) : String
	{
		return _sEncoding;
	}

	public function setDebug( bDebug : Boolean ) : Void
	{
		_bDebug = bDebug;
	}

	public function getDebug( Void ) : Boolean
	{
		return _bDebug;
	}

	public function getServiceName() : String	{		return _sServiceName;	}
	
	public function getMethod() : String	{		return _sMethod;	}
	
	public function load() : Void
	{
		// look http://www.swxformat.org
		var mcConnection : MovieClip = MovieClip(getContent());
		mcConnection.serviceClass = getServiceName();		mcConnection.method = getMethod();		mcConnection.args = JsonSerialize.serialize(arguments);		mcConnection.debug = _bDebug;
		// --
		mcConnection.loadMovie(getURL(), getEncoding()); 
		super.load();
	}

	/**
	 * override
	 */
	public function onLoadInit() : Void
	{
		fireEventType(onLoadProgressEVENT);
		var oRaw = this.getContent().result;
		if (oRaw.error)
		{
			_oEB.broadcastEvent(new SWXFaultEvent(onFaultEVENT, oRaw.message, oRaw.code, getMethod()));
		}
		else
		{
			_oEB.broadcastEvent(new SWXResultEvent(onResultEVENT, oRaw, getMethod()));
		}
		fireEventType(onLoadInitEVENT);	
		super.release();
	}	

	public function addListener(oL : ISWXListener) : Void
	{
		_oEB.addListener(oL);
	}

	public function removeListener(oL : ISWXListener) : Void
	{
		_oEB.removeListener(oL);
	}}